package cn.pzaily.orange.manage.service.impl;

import cn.pzaily.orange.core.utils.page.MybatisPageHelper;
import cn.pzaily.orange.core.utils.page.PageRequest;
import cn.pzaily.orange.core.utils.page.PageResponse;
import cn.pzaily.orange.core.utils.PoiUtils;
import cn.pzaily.orange.core.utils.StringUtils;
import cn.pzaily.orange.manage.dao.SysUserMapper;
import cn.pzaily.orange.manage.model.SysMenu;
import cn.pzaily.orange.manage.model.SysUser;
import cn.pzaily.orange.manage.model.SysUserExample;
import cn.pzaily.orange.manage.service.SysMenuService;
import cn.pzaily.orange.manage.service.SysUserService;
import cn.pzaily.orange.manage.utils.SecurityUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.util.*;

/**
 * @PackageName: cn.pzaily.orange.server.impl
 * @Description:
 * @Author: Pang
 * @Date: 2021-05-09 22:58
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private SysMenuService menuService;

    @Override
    public PageResponse findPage(PageRequest param) {
        return MybatisPageHelper.selectPage(param, userMapper);
    }

    @Override
    public List<SysUser> findAll() {
        return null;
    }

    @Override
    public boolean save(SysUser data) {
        String password = data.getPassword();
        data.setPassword(SecurityUtils.encryptPassword(password));
        int count = userMapper.insertSelective(data);
        return true;
    }

    @Override
    public boolean modify(SysUser data) {
        return false;
    }

    @Override
    public SysUser findById(Long id) {
        return null;
    }

    @Override
    public boolean removeByIds(List<Long> ids) {
        return false;
    }

    @Override
    public File createUserExcelFile(PageRequest request) {
        PageResponse pageResponse = findPage(request);
        File excelFile = createUserExcelFile(pageResponse.getContent());
        return excelFile;
    }

    @Override
    public SysUser findByUserName(String s) {
        SysUserExample userExample = new SysUserExample();
        userExample.createCriteria().andNameEqualTo(s);
        List<SysUser> sysUsers = userMapper.selectByExample(userExample);
        if (CollectionUtils.isEmpty(sysUsers)) {
            return null;
        }
        return sysUsers.get(0);
    }

    @Override
    public Set<String> findPermissionsByUserName(String name) {
        Set<String> permsSet = new HashSet<>();
        List<SysMenu> menus = menuService.findByUserName(name);
        for (SysMenu menu : menus) {
            String perms = menu.getPerms();
            if (StringUtils.isNotEmpty(perms)) {
                permsSet.add(perms);
            }
        }
        return permsSet;
    }

    private File createUserExcelFile(List<?> records) {
        if (null == records) {
            records = new ArrayList<>();
        }
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        Row rowOne = sheet.createRow(0);
        int columnIndex = 0;
        rowOne.createCell(columnIndex).setCellValue("No");
        rowOne.createCell(++columnIndex).setCellValue("编号");
        rowOne.createCell(++columnIndex).setCellValue("用户名");
        rowOne.createCell(++columnIndex).setCellValue("昵称");
        rowOne.createCell(++columnIndex).setCellValue("机构");
        rowOne.createCell(++columnIndex).setCellValue("角色");
        rowOne.createCell(++columnIndex).setCellValue("邮箱");
        rowOne.createCell(++columnIndex).setCellValue("手机号");
        rowOne.createCell(++columnIndex).setCellValue("状态");
        rowOne.createCell(++columnIndex).setCellValue("头像");
        rowOne.createCell(++columnIndex).setCellValue("创建人");
        rowOne.createCell(++columnIndex).setCellValue("创建时间");
        rowOne.createCell(++columnIndex).setCellValue("更新人");
        rowOne.createCell(++columnIndex).setCellValue("更新时间");

        for (int i = 0; i < records.size(); i++) {
            SysUser user = (SysUser) records.get(i);
            Row row = sheet.createRow(i + 1);

            for (int j = 0; j < columnIndex + 1; j++) {
                row.createCell(j);
            }

            columnIndex = 0;
            row.getCell(columnIndex).setCellValue(i + 1);
            row.getCell(++columnIndex).setCellValue(user.getId());
            row.getCell(++columnIndex).setCellValue(user.getName());
            row.getCell(++columnIndex).setCellValue(user.getNickName());
            row.getCell(++columnIndex).setCellValue(user.getName());
            row.getCell(++columnIndex).setCellValue(user.getId());
            row.getCell(++columnIndex).setCellValue(user.getName());
        }

        return PoiUtils.createExcelFile(workbook, "download_user");
    }
}
